1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 import java.io.File;
36 import java.io.FileOutputStream;
37 import java.nio.charset.Charset;
38
39 public class Setup {
40
41 public static void main(String[] args) throws Exception {
42 if (args.length < 2) {
43 System.err.println("Usage: java Setup <work-dir> <premain-class>");
44 return;
45 }
46 String workDir = args[0];
47 String premainClass = args[1];
48
49 String manifestFile = workDir + fileSeparator + "MANIFEST.MF";
50 String bootClassPath = "boot" + suffix();
51
52 String bootDir = workDir + fileSeparator + bootClassPath;
53
54
55
56
57
58 File f = new File(bootDir);
59 f.mkdir();
60
61
62
63
64
65 try (FileOutputStream out = new FileOutputStream(manifestFile)) {
66 out.write("Manifest-Version: 1.0\n".getBytes("UTF-8"));
67
68 byte[] premainBytes =
69 ("Premain-Class: " + premainClass + "\n").getBytes("UTF-8");
70 out.write(premainBytes);
71
72 out.write( "Boot-Class-Path: ".getBytes("UTF-8") );
73
74 byte[] value = bootClassPath.getBytes("UTF-8");
75 for (int i=0; i<value.length; i++) {
76 int v = (int)value[i];
77 if (v < 0) v += 256;
78 byte[] escaped =
79 ("%" + Integer.toHexString(v)).getBytes("UTF-8");
80 out.write(escaped);
81 }
82 out.write( "\n\n".getBytes("UTF-8") );
83 }
84
85
86
87
88 f = new File(workDir + fileSeparator + "boot.dir");
89 try (FileOutputStream out = new FileOutputStream(f)) {
90 out.write(bootDir.getBytes(defaultEncoding));
91 }
92 }
93
94
95
96 private static final String fileSeparator = System.getProperty("file.separator");
97 private static final String osName = System.getProperty("os.name");
98 private static final String defaultEncoding = Charset.defaultCharset().name();
99
100
101 private static final String arabic = "\u0627\u0644\u0639\u0631\u0628\u064a\u0629";
102 private static final String s_chinese = "\u4e2d\u6587";
103 private static final String t_chinese = "\u4e2d\u6587";
104 private static final String russian = "\u0440\u0443\u0441\u0441\u043A\u0438\u0439";
105 private static final String hindi = "\u0939\u093f\u0902\u0926\u0940";
106 private static final String greek = "\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac";
107 private static final String hebrew = "\u05e2\u05d1\u05e8\u05d9\u05ea";
108 private static final String japanese = "\u65e5\u672c\u8a9e";
109 private static final String korean = "\ud55c\uad6d\uc5b4";
110 private static final String lithuanian = "Lietuvi\u0173";
111 private static final String czech = "\u010de\u0161tina";
112 private static final String turkish = "T\u00fcrk\u00e7e";
113 private static final String spanish = "espa\u00f1ol";
114 private static final String thai = "\u0e44\u0e17\u0e22";
115 private static final String unicode = arabic + s_chinese + t_chinese
116 + russian + hindi + greek + hebrew + japanese + korean
117 + lithuanian + czech + turkish + spanish + thai;
118
119 private static String suffix() {
120
121
122
123
124
125 String[][] names = {
126 { "UTF-8", unicode, "" },
127 { "windows-1256", null, "" },
128 { "iso-8859-6", arabic, null },
129 { "GBK", s_chinese, s_chinese },
130 { "GB18030", s_chinese, s_chinese },
131 { "GB2312", s_chinese, null },
132 { "x-windows-950", null, t_chinese },
133 { "x-MS950-HKSCS", null, t_chinese },
134 { "x-euc-tw", t_chinese, null },
135 { "Big5", t_chinese, null },
136 { "Big5-HKSCS", t_chinese, null },
137 { "windows-1251", null, "" },
138 { "iso-8859-5", russian, null },
139 { "koi8-r", russian, null },
140 { "windows-1253", null, "" },
141 { "iso-8859-7", greek, null },
142 { "windows-1255", null, "" },
143 { "iso8859-8", hebrew, null },
144 { "windows-31j", null, japanese },
145 { "x-eucJP-Open", japanese, null },
146 { "x-EUC-JP-LINUX", japanese, null },
147 { "x-pck", japanese, null },
148 { "x-windows-949", null, korean },
149 { "euc-kr", korean, null },
150 { "windows-1257", null, "" },
151 { "iso-8859-13", lithuanian, null },
152 { "windows-1250", null, "" },
153 { "iso-8859-2", czech, null },
154 { "windows-1254", null, "" },
155 { "iso-8859-9", turkish, null },
156 { "windows-1252", null, "" },
157 { "iso-8859-1", spanish, null },
158 { "iso-8859-15", spanish, null },
159 { "x-windows-874", null, thai },
160 { "tis-620", thai, null },
161 };
162
163 int column;
164 if (osName.startsWith("Windows")) {
165 column = 2;
166 } else {
167 column = 1;
168 }
169 for (int i = 0; i < names.length; i++) {
170 if (names[i][0].equalsIgnoreCase(defaultEncoding)) {
171 return names[i][column];
172 }
173 }
174 return "";
175 }
176 }